/*
 * @Author: 
 * @Date: 2025-06-11 14:12:20
 * @LastEditTime: 2025-10-14 14:00:00
 * @Description: 内容居中且徽章闪烁的菜单按钮组件
 */
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:wms/screens/system/fontsize_provider.dart';
import 'package:provider/provider.dart';

// 字体缩放因子，统一控制所有文本大小
late double fontScale;

// 将MenuButton改为StatefulWidget以支持动画
class MenuButton extends StatefulWidget {
  final IconData icon;
  final String label;
  final Color color;
  final VoidCallback onTap;
  final int? badgeCount; // 徽章数量

  const MenuButton({
    super.key,
    required this.icon,
    required this.label,
    required this.color,
    required this.onTap,
    this.badgeCount, // 徽章数量参数
  });

  @override
  State<MenuButton> createState() => _MenuButtonState();
}

class _MenuButtonState extends State<MenuButton> with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _opacityAnimation;

  @override
  void initState() {
    super.initState();
    
    // 初始化动画控制器，实现闪烁效果
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 800),
    );
    
    // 透明度动画，从1到0.3再回到1，循环执行
    _opacityAnimation = Tween<double>(begin: 1.0, end: 0.3).animate(
      CurvedAnimation(
        parent: _animationController,
        curve: Curves.easeInOut,
      ),
    )..addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _animationController.reverse();
      } else if (status == AnimationStatus.dismissed) {
        _animationController.forward();
      }
    });
    
    // 只有当有未读数量时才开始动画
    if (widget.badgeCount != null && widget.badgeCount! > 0) {
      _animationController.forward();
    }
  }

  @override
  void didUpdateWidget(covariant MenuButton oldWidget) {
    super.didUpdateWidget(oldWidget);
    // 当徽章数量变化时控制动画
    if (widget.badgeCount != null && widget.badgeCount! > 0) {
      if (!_animationController.isAnimating) {
        _animationController.forward();
      }
    } else {
      _animationController.stop();
    }
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    fontScale = Provider.of<FontSizeProvider>(context).fontScale;
    return Card(
      elevation: 4,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      child: InkWell(
        borderRadius: BorderRadius.circular(12),
        onTap: widget.onTap,
        // 使用Stack包裹，设置alignment为center确保内容居中
        child: Stack(
          alignment: Alignment.center, // 关键：让子组件居中对齐
          children: [
            // 按钮原有内容，保持居中
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(widget.icon, size: 48, color: widget.color),
                  const SizedBox(height: 8),
                  Text(
                    widget.label,
                    style: TextStyle(
                      fontSize: 18 * fontScale,
                      fontWeight: FontWeight.bold,
                    ),
                    textAlign: TextAlign.center,
                  ),
                ],
              ),
            ),
            // 徽章显示在整个按钮的右上角，添加闪烁动画
            if (widget.badgeCount != null && widget.badgeCount! > 0)
              Positioned(
                top: 5, // 距离按钮顶部的距离
                right: 5, // 距离按钮右侧的距离
                child: FadeTransition(
                  opacity: _opacityAnimation,
                  child: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 1),
                    decoration: BoxDecoration(
                      color: Colors.red,
                      borderRadius: BorderRadius.circular(10),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black12,
                          blurRadius: 2,
                          spreadRadius: 1,
                        )
                      ]
                    ),
                    constraints: const BoxConstraints(
                      minWidth: 18,
                      minHeight: 18,
                    ),
                    child: Text(
                      widget.badgeCount.toString(),
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 12 * fontScale,
                        fontWeight: FontWeight.bold,
                      ),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
}
